package csc.daonjpa.java.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.BaseInfo;


public class BaseInfoDAO {

	  @PersistenceContext
	  private EntityManager em;
	/**
	 * get BaseInfo by primary key.
	 * @param id
	 * @return
	 */
	 @Transactional
	public BaseInfo getBaseInfo(Long id) {
//		EntityManager em = TransactionManager.getEntityManager();
		BaseInfo baseInfo = em.find(BaseInfo.class, id);
		return baseInfo;
	}
	/**
	 * update available amount of account after transfer money.
	 * @param availableAmount
	 * @param id
	 * @return
	 */
	 @Transactional
	public boolean updateBaseInfo(Double availableAmount, Long id) {
		String temp = "UPDATE " + BaseInfo.class.getName()
				+ " SET availableAmount = :availableAmount WHERE id = :id";
//		EntityManager em = TransactionManager.getEntityManager();
		Query query = em.createQuery(temp);
		query.setParameter("availableAmount", availableAmount);
		query.setParameter("id", id);
		return query.executeUpdate() > 0;
	}
}
